package com.lc.user.express.my;

import android.content.Context;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lc.user.express.BaseSecondActivity;
import com.lc.user.express.R;
import com.lc.user.express.httpserver.GetBankCard;
import com.lc.user.express.httpserver.GetVerfyCode;
import com.lc.user.express.model.BankModel;
import com.lc.user.express.utils.GetToast;
import com.lc.user.express.utils.PublicMethod;
import com.lc.user.express.view.SelectBankPopupWindow;
import com.zcx.helper.http.AsyCallBack;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/3/15.添加银行卡
 */
public class AddBankCardActivity extends BaseSecondActivity {
    private SelectBankPopupWindow popup;// 自定义底部弹出框
    private List<BankModel> bankList = new ArrayList<BankModel>();//银行列表
    private TextView tv_bank;//银行
    private TextView et_name;//姓名
    private EditText et_mobile;//手机号
    private EditText et_verify_code;//验证码
    private EditText et_bankcard_num;//银行卡号
    private TextView tv_getcode;//获取验证码
    private TimeCount time;//倒计时
    private String code = "";
    private String phonenumber;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_bankcard);
        setTitle("添加银行卡");
        initView();
    }

    private void initView() {
        ((TextView) findViewById(R.id.tv_public)).setOnClickListener(this);
        ((RelativeLayout) findViewById(R.id.rl_bank)).setOnClickListener(this);
        tv_bank = (TextView) findViewById(R.id.tv_bank);
        tv_getcode = (TextView) findViewById(R.id.tv_getcode);
        et_verify_code = (EditText) findViewById(R.id.et_verify_code);
        et_mobile = (EditText) findViewById(R.id.et_mobile);
        et_name = (EditText) findViewById(R.id.et_name);
        et_bankcard_num = (EditText) findViewById(R.id.et_bankcard_num);
        tv_getcode.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_public:
                if (ifCommit()) {
                    Bundle bundle = new Bundle();
                    bundle.putString("name", et_name.getText().toString().trim());
                    bundle.putString("bankcard_num", et_bankcard_num.getText().toString().trim());
                    bundle.putString("mobile", et_mobile.getText().toString().trim());
                    bundle.putString("bank", tv_bank.getText().toString());
                    show(SetPayPasswordActivity.class, SetPayPasswordActivity.TYPE_SET, bundle);
                }
                break;
            case R.id.rl_bank:
                showBanksView();
                break;
            case R.id.tv_getcode:
                phonenumber = et_mobile.getText().toString();
                getCode();
                break;
        }
    }

    private void showBanksView() {
        if (getCurrentFocus() != null) {
            ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
        popup = new SelectBankPopupWindow(this, bankList, "选择银行", 1);
        popup.showAtLocation(findViewById(R.id.ll_container), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        popup.setOnSelectListener(new SelectBankPopupWindow.OnSelectListener() {
            @Override
            public void onSelect(int position) {

                tv_bank.setText(bankList.get(position).getName());
                popup.dismiss();
                for (int i = 0; i < bankList.size(); i++) {
                    if (i == position) {
                        bankList.get(i).setIsChoosed(true);
                    } else {
                        bankList.get(i).setIsChoosed(false);
                    }
                }
            }
        });
        if (bankList.size() <= 0) {
            getBanks();
        }
    }

    private void getBanks() {
        new GetBankCard(new AsyCallBack<GetBankCard.Info>() {
            @Override
            public void onSuccess(String toast, int type, GetBankCard.Info info) throws Exception {
                bankList.addAll(info.dataList);
                popup.refreshList(bankList);
            }
        }).execute(cnt);

    }

    private boolean ifCommit() {
        if (TextUtils.isEmpty(et_name.getText().toString())) {
            GetToast.useString(cnt, "请输入持卡人姓名");
            return false;
        }
        if (TextUtils.isEmpty(et_bankcard_num.getText().toString())) {
            GetToast.useString(cnt, "请输入卡号");
            return false;
        }
        if (!PublicMethod.checkBankCard(et_bankcard_num.getText().toString().trim())) {
            GetToast.useString(cnt, "银行卡号错误");
            return false;
        }
        if (TextUtils.isEmpty(tv_bank.getText().toString())) {
            GetToast.useString(cnt, "请选择卡类型");
            return false;
        }
        if (TextUtils.isEmpty(et_mobile.getText().toString())) {
            GetToast.useString(cnt, "请输入银行预留的手机号码");
            return false;
        }

        if (!PublicMethod.isMobilePhone(et_mobile.getText().toString().trim())) {
            GetToast.useString(cnt, "请输入正确的手机号码");
            return false;
        }
        if (TextUtils.isEmpty(et_verify_code.getText().toString())) {
            GetToast.useString(cnt, "请输入验证码");
            return false;
        }
        if (!code.equals(et_verify_code.getText().toString())) {
            GetToast.useString(cnt, "验证码错误");
            return false;
        }
        if (!et_mobile.getText().toString().trim().equals(phonenumber)) {
            GetToast.useString(cnt, "请使用获取验证码的手机号");
            return false;
        }
        return true;
    }

    private void getCode() {
        new GetVerfyCode(et_mobile.getText().toString().trim(), new AsyCallBack<GetVerfyCode.Info>() {
            @Override
            public void onSuccess(String toast, int type, GetVerfyCode.Info info) throws Exception {
                code = info.code;
                time = new TimeCount(60000, 1000, false);
                time.start();
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                GetToast.useString(cnt, toast);
            }
        }).execute(cnt);

    }

    class TimeCount extends CountDownTimer {

        public TimeCount(long millisInFuture, long countDownInterval, boolean ten) {
            super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
        }

        @Override
        public void onFinish() {// 计时完毕时触发
            tv_getcode.setClickable(true);
            tv_getcode.setText("获取验证码");
        }

        @Override
        public void onTick(long millisUntilFinished) {// 计时过程显示
            tv_getcode.setClickable(false);
            tv_getcode.setText(millisUntilFinished / 1000 + "s");

        }
    }
}
